package com.yiwyn;

import org.apache.commons.cli.*;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class CommandLineApp {
    public static void main(String[] args) {
        // 创建Options对象
        Options options = new Options();

        // 定义选项 -g (greet) 和 -n (name)
        Option greetOption = new Option("g", "greet", false, "Trigger a greeting");
        greetOption.setRequired(false);
        options.addOption(greetOption);

        Option nameOption = new Option("n", "name", true, "Provide a name");
        nameOption.setRequired(false);
        options.addOption(nameOption);

        Option foldOption = new Option("f", "fold", false, "Provide a name");
        nameOption.setRequired(false);
        options.addOption(foldOption);

        CommandLineParser parser = new DefaultParser();
        HelpFormatter formatter = new HelpFormatter();
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter commands. Type 'exit' to quit.");

        while (true) {
            // 获取用户输入
            System.out.print("> ");
            String input = scanner.nextLine();

            // 如果用户输入 'exit' 则退出
            if ("exit".equalsIgnoreCase(input)) {
                break;
            }

            // 将输入拆分为参数
            String[] commandArgs = input.split(" ");

            try {
                CommandLine cmd = parser.parse(options, commandArgs);


                if (cmd.hasOption("g")) {
                    String name = cmd.getOptionValue("n", "World");
                    System.out.println("Hello, " + name + "!");
                } else if (cmd.hasOption("f")) {
                    // 创建一个JFrame窗口作为父窗口
                    JFrame frame = new JFrame();
                    frame.setAlwaysOnTop(true);  // 设置父窗口始终置顶
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setSize(300, 200);

                    // 使用JFileChooser选择文件夹
                    JFileChooser fileChooser = new JFileChooser();
                    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

                    // 显示JFileChooser对话框，传入frame作为父窗口
                    int result = fileChooser.showOpenDialog(frame);

                    if (result == JFileChooser.APPROVE_OPTION) {
                        File selectedFolder = fileChooser.getSelectedFile();
                        System.out.println("Selected folder: " + selectedFolder.getAbsolutePath());
                    }

                    // 关闭父窗口
                    frame.dispose();

                } else {
                    System.out.println("No valid command found. Use -g to greet.");
                }

            } catch (ParseException e) {
                e.printStackTrace();
                formatter.printHelp("utility-name", options);
            }
        }

        scanner.close();
    }
}
